package WebServices
{
	import AutoCompletes.AcaInstName;
	import AutoCompletes.ProInstName;
	import AutoCompletes.ProFieldName;
	import AutoCompletes.ProQualName;
	import AutoCompletes.UniInstName;
	import AutoCompletes.UniFieldName;
	import AutoCompletes.UniQualName;
	import AutoCompletes.TrainingInstName;
	import AutoCompletes.TrainingFieldName;
	import flash.net.URLVariables;
	
	public class AutoCompleteWebService extends HttpResponder
	{
		

		
		public var which:String;
		static private var mainFix:AutoCompleteWebService = new AutoCompleteWebService(null, null, null);
	
		public function AutoCompleteWebService(auth:AuthenticationData, success:Function, failure:Function)
		{
			super(auth, success, failure);
		}
	

		static public function get instance():AutoCompleteWebService
		{
			return mainFix;						
		}
		public function callServer(service:SkillsAuditService, aString:String, index:int, tableName:String, rowName:String, dataFieldName:String, dataName:String, whichIs:String):void
		{
			var xml:XML = 
			<getFieldsValue>
				<SecurityContext>
					<UserName/>
					<UserPasswordMD5Hash/>
				</SecurityContext>
				<searchValue>
				</searchValue>
				<index>
				</index>
				<tableName>
				</tableName>
				<rowName>
				</rowName>
				<dataFieldName>
				</dataFieldName>
				<dataName>
				</dataName>
			</getFieldsValue>
			this.which = whichIs;
			xml.SecurityContext.UserName = m_authData.username;
			xml.SecurityContext.UserPasswordMD5Hash = m_authData.getDigest();
			xml.searchValue = aString;
			xml.index = index;
			xml.tableName = tableName;
			xml.rowName = rowName;
			xml.dataFieldName = dataFieldName;
			xml.dataName = dataName;
			service.setCallbacks(onSuccessforLists, onFailure);
			var strXML:String = "xml="+xml.toXMLString();
			var myPattern:RegExp = /&/g;  
			strXML = strXML.replace(myPattern, "%26"); 
			service.request= new URLVariables(strXML);
			service.send();
					 
		}		
		override protected function onFailure(err:Error):void
		{
			m_fnFailure();
			m_fnSuccess = null;
			m_fnFailure = null;
		}																						
	    override protected function onSuccess(xmlResponse:XML):void
		{
			if(xmlResponse.name().toString() != "EnumDataListResponse")
			{
				onFailure(new Error("Invalid root element received from server. Expected 'EnumDataListResponse' received '" +xmlResponse.name().toString() + "'"));
				return;
			}
			if (this.which == "Academic Institution"){
				AcaInstName.instance.onSuccess(xmlResponse);
			}
			else if (this.which == "Professional Institution"){
				ProInstName.instance.onSuccess(xmlResponse);
			}
			else if (this.which == "Professional Field"){
				ProFieldName.instance.onSuccess(xmlResponse);
			}
			else if (this.which == "Professional Qualification"){
				ProQualName.instance.onSuccess(xmlResponse);
			}
			else if (this.which == "University Institution"){
				UniInstName.instance.onSuccess(xmlResponse);
			}
			else if (this.which == "University Field"){
				UniFieldName.instance.onSuccess(xmlResponse);
			}
			else if (this.which == "University Qualification"){
				UniQualName.instance.onSuccess(xmlResponse);
			}
			else if (this.which == "Training Institution"){
				TrainingInstName.instance.onSuccess(xmlResponse);
			}
			else if (this.which == "Training Field"){
				TrainingFieldName.instance.onSuccess(xmlResponse);
			}
			super.onSuccess(xmlResponse);
		}

	}
}